package create.singleton.singleton_demo2;

/**
 * @author yeyangtao created at 11:32 2020/11/5
 */
interface Resource {
    int getValue();

    void serValue(int x);
}

/**
 * 由于这不是从Cloneable基类继承而且没有添加可克隆性，
 * 因此将其设置为final可防止通过继承添加可克隆性。
 * 这也实现了线程安全的延迟初始化：
 */
final class Singleton {
    private static final class ResourceImpl implements Resource {
        private int i;

        public ResourceImpl(int i) {
            this.i = i;
        }

        @Override
        public synchronized int getValue() {
            return i;
        }

        @Override
        public synchronized void serValue(int x) {
            i = x;
        }
    }

    private static class ResourceHolder {
        private static final Resource resource = new ResourceImpl(47);
    }

    public static Resource getResource() {
        return ResourceHolder.resource;
    }
}

public class SingletonPattern {
}
